<?php

namespace App\Task\Stat;

use App\Model\AppChannel;
use App\Model\CallBase;
use App\Model\Stat\StatDailyCall;
use App\Model\VideoCall;
use App\Model\VoiceCall;
use Hyperf\Crontab\Annotation\Crontab;

/**
 * 每日话单统计
 * @Crontab(name="StatDailyCallTask", rule="10,25,40,55 *\/1 * * *", callback="execute", memo="每日话单统计", singleton=false)
 */
class StatDailyCallTask
{
    /**
     * @return void
     */
    public function execute()
    {
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));
        // $this->runDaily();
        // for ($day = 40; $day > 0; $day--) {
        //     $this->runDaily($day);
        // }
    }

    /**
     * 日报相关数据
     */
    function runDaily($day = 0)
    {
        $channels = AppChannel::getAllChannels();
        //一天前5分钟内
        if (time() - strtotime(date('Y-m-d')) < 1200) $day++;
        $this->dt = date('Y-m-d', strtotime("-{$day} day"));
        $this->endTime = date('Y-m-d', strtotime("-" . ($day - 1) . " day"));
        $channels[] = 'all';
        foreach ($channels as $channel) {
            //话单统计
            $this->beginStatCall(VideoCall::CALL_TYPE, $channel);
            // $this->beginStatCall(VoiceCall::CALL_TYPE);
        }
    }

    /**
     * 获取话单模型
     */
    function getCallModel($statClass, $callType = -1, $subType = 0)
    {
        $where = [
            'dt' => $this->dt,
            'call_type' => $callType,
            'call_subtype' => $subType
        ];
        $model = $statClass::where($where)->first();
        if (!$model) $model = new $statClass();
        return $model;
    }

    /**
     * 话单日报
        $table->tinyInteger('call_type')->default(-1)->comment('话单类型');
        $table->integer('call_num')->default(0)->comment('总话单');
        $table->integer('success_num')->default(0)->comment('成功话单');
        $table->integer('caller_num')->default(0)->comment('总呼叫人数');
        $table->integer('receiver_num')->default(0)->comment('总被叫人数');
        $table->integer('success_caller_num')->default(0)->comment('成功拨打人数');
        $table->integer('success_receiver_num')->default(0)->comment('成功接听人数');
        $table->integer('total_call_time')->default(0)->comment('话单总时长');
        $table->integer('call_num_fail')->default(0)->comment('失败话单数');
        $table->integer('call_num_noanswer')->default(0)->comment('无人接听话单数');
        $table->integer('call_num_hangup')->default(0)->comment('主叫挂断');
        $table->integer('call_num_reject')->default(0)->comment('被叫拒绝');
        $table->integer('call_num_err')->default(0)->comment('系统异常挂断');
     */
    function beginStatCall($callType = -1)
    {
        $callCls = $callType == -1 ? CallBase::class : ($callType == 1 ? VideoCall::class : VoiceCall::class);
        foreach (StatDailyCall::SUB_TYPE as $type => $subType) {
            $model = $this->getCallModel(StatDailyCall::class, $callType, $type);
            if (!$model) continue;
            $model->dt = $this->dt;
            // $model->channel = $channel;
            // $channel == 'all' && $channel = '';
            $model->call_type = $callType;
            $model->call_subtype = $type;
            $callFrom = $subType['from'];
            $model->call_num = $callCls::getCallNum('all', $this->dt, $this->endTime, $callFrom);
            $model->success_num = $callCls::getCallNum('success_num', $this->dt, $this->endTime, $callFrom);
            $model->caller_num = $callCls::getCallNum('caller_num', $this->dt, $this->endTime, $callFrom);
            $model->receiver_num = $callCls::getCallNum('receiver_num', $this->dt, $this->endTime, $callFrom);
            $model->success_caller_num = $callCls::getCallNum('success_caller_num', $this->dt, $this->endTime, $callFrom);
            $model->success_receiver_num = $callCls::getCallNum('success_receiver_num', $this->dt, $this->endTime, $callFrom);
            $model->total_call_time = $callCls::getCallNum('total_call_time', $this->dt, $this->endTime, $callFrom);
            $model->total_call_minutes = $callCls::getCallNum('total_call_minutes', $this->dt, $this->endTime, $callFrom);
            $model->call_num_fail = $callCls::getCallNum('call_num_fail', $this->dt, $this->endTime, $callFrom);
            $model->call_num_noanswer = $callCls::getCallNum('call_num_noanswer', $this->dt, $this->endTime, $callFrom);
            $model->call_num_hangup = $callCls::getCallNum('call_num_hangup', $this->dt, $this->endTime, $callFrom);
            $model->call_num_reject = $callCls::getCallNum('call_num_reject', $this->dt, $this->endTime, $callFrom);
            $model->call_num_err = $callCls::getCallNum('call_num_err', $this->dt, $this->endTime, $callFrom);
            $model->saveOrFail();
        }
        // log_info(__CLASS__ . "==beginStatCall==" . json_encode($model));
        return;
    }
}
